﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.ResourceManagement.AsyncOperations;
using UnityEngine.ResourceManagement.ResourceProviders;
using UnityEngine.SceneManagement;

public class SABMananger
{
    /// <summary>是否启动编辑器模式</summary>
    public static bool EditorModeEnable = true;

    static Dictionary<string, bool> keyDic = new Dictionary<string, bool>();

    static bool HaveKey(string key)
    {
        bool have;
        if (!keyDic.TryGetValue(key, out have))
            keyDic.Add(key, true);

        return have;
    }

    public static AsyncOperationHandle<GameObject> InstantiateAsync(object key, Transform parent = null, bool instantiateInWorldSpace = false, bool trackHandle = true)
    {
        string path = (string)key;
        if (!HaveKey(path))
        {
            SDynamicLocator.AddObjectItem(path, null, EditorModeEnable);
        }

        return Addressables.InstantiateAsync(path, parent, instantiateInWorldSpace, trackHandle);
    }

    //===============================================Scene===============================================================
    public static AsyncOperationHandle<SceneInstance> LoadSceneAsync(object key, LoadSceneMode loadMode = LoadSceneMode.Single, bool activateOnLoad = true, int priority = 100)
    {
        string path = (string)key;
        if (!HaveKey(path))
        {
            SDynamicLocator.AddSceneItem(path, null, EditorModeEnable);
        }

        return Addressables.LoadSceneAsync(path, loadMode, activateOnLoad, priority);
    }
}
